<template>
  <div>
    <van-nav-bar :title="$t('title')" left-arrow @click-left="$router.back()" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        :label="$t('email')"
        :placeholder="$t('email_please')"
        :rules="[{ required: true, message: $t('email_please') }]"
      />
      <van-field
        v-model="verification_code"
        clearable
        :label="$t('code')"
        :placeholder="$t('code_please')"
        :rules="[{ required: true, message: $t('code_please') }]"
      >
        <template #button>
          <van-button size="small" type="primary" block @click.prevent="handleGetCode">{{ $t('send') }}</van-button>
        </template>
      </van-field>
      <div style="margin: 15px">
        <van-button block type="info"  native-type="submit" class="submit van-button-blue">
          {{ $t('actions.submit') }}
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { isEmail } from '@/utils/validator'
export default {
  i18n: {
    messages: {
      zh: {
        title: '绑定邮箱',
        email: '邮箱',
        email_please: '请填写邮箱',
        code: '验证码',
        code_please: '请填写验证码',
        send: '发送验证码'
      },
      en: {
        title: 'Binding Email',
        email: 'Email',
        email_please: 'Please fill in the email address',
        code: 'Code',
        code_please: 'Please fill in the code',
        send: 'Send Code'
      },
      jp: {
        title: 'メールボックスをバインド',
        email: 'メールボックス',
        email_please: 'メールボックスに記入してください',
        code: '認証コード',
        code_please: '検証コードを記入してください',
        send: '認証コードを送信'
      },
      kr: {
        title: '사서함 바인딩',
        email: '우편',
        email_please: '이메일을 입력하세요',
        code: '확인 코드',
        code_please: '인증번호를 입력해주세요',
        send: '인증 코드 보내기'
      },
    }
  },
  data () {
    return {
      username: '',
      verification_code: '',
      times: 60
    }
  },
  methods: {
    ...mapActions({
      bindEmail: 'user/bindEmail',
      getUserInfo: 'user/getUserInfo',
      getCode: 'user/getCode'
    }),
    handleGetCode () {
      if (isEmail(this.username)) {
        this.getCode(this.username)
          .then(() => {
            this.getTime()
          })
          .catch(({ msg }) => {
            this.$toast(msg)
          })
      } else {
        this.$toast('邮箱号格式不正确')
      }
    },
    getTime () {
      this.timer = setInterval(() => {
        this.times--
        if (this.times === 0) {
          clearInterval(this.timer)
          this.times = 60
        }
      }, 1000)
    },
    onSubmit () {
      this.bindEmail().then(() => {
        this.getUserInfo()
        this.$router.back()
      })
    }
  }
}
</script>
